package health_system.health_system.websocket.Test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 统一通信配置类
 * 管理WebRTC和WebSocket相关配置
 */
@Component
@Configuration
public class MediaCommunicationServerConfig {

    @Value("${unified.communication.websocket.endpoint:/UnifiedCommunication}")
    private String websocketEndpoint;

    @Value("${unified.communication.stun.servers:stun:stun.l.google.com:19302,stun:stun1.l.google.com:19302}")
    private String stunServers;

    @Value("${unified.communication.turn.servers:}")
    private String turnServers;

    @Value("${unified.communication.turn.username:}")
    private String turnUsername;

    @Value("${unified.communication.turn.credential:}")
    private String turnCredential;

    @Value("${unified.communication.max.bitrate:2000}")
    private int maxBitrate;

    @Value("${unified.communication.video.codec:VP8}")
    private String videoCodec;

    @Value("${unified.communication.audio.codec:OPUS}")
    private String audioCodec;

    /**
     * 获取WebSocket端点路径
     * @return WebSocket端点路径
     */
    public String getWebsocketEndpoint() {
        return websocketEndpoint;
    }

    /**
     * 获取STUN服务器列表
     * @return STUN服务器列表
     */
    public List<String> getStunServers() {
        return Arrays.asList(stunServers.split(","));
    }

    /**
     * 获取TURN服务器列表
     * @return TURN服务器列表
     */
    public List<String> getTurnServers() {
        if (turnServers == null || turnServers.isEmpty()) {
            return List.of();
        }
        return Arrays.asList(turnServers.split(","));
    }

    /**
     * 获取TURN用户名
     * @return TURN用户名
     */
    public String getTurnUsername() {
        return turnUsername;
    }

    /**
     * 获取TURN凭证
     * @return TURN凭证
     */
    public String getTurnCredential() {
        return turnCredential;
    }

    /**
     * 获取最大比特率
     * @return 最大比特率
     */
    public int getMaxBitrate() {
        return maxBitrate;
    }

    /**
     * 获取视频编解码器
     * @return 视频编解码器
     */
    public String getVideoCodec() {
        return videoCodec;
    }

    /**
     * 获取音频编解码器
     * @return 音频编解码器
     */
    public String getAudioCodec() {
        return audioCodec;
    }

    /**
     * 获取ICE服务器配置的JSON字符串
     * @return ICE服务器配置的JSON字符串
     */
    public String getIceServersJson() {
        StringBuilder json = new StringBuilder("[");
        
        // 添加STUN服务器
        List<String> stunList = getStunServers();
        for (int i = 0; i < stunList.size(); i++) {
            if (i > 0) {
                json.append(",");
            }
            json.append("{\"urls\":\"").append(stunList.get(i)).append("\"}");
        }
        
        // 添加TURN服务器
        List<String> turnList = getTurnServers();
        if (!turnList.isEmpty() && !turnUsername.isEmpty()) {
            if (!stunList.isEmpty()) {
                json.append(",");
            }
            
            for (int i = 0; i < turnList.size(); i++) {
                if (i > 0) {
                    json.append(",");
                }
                json.append("{\"urls\":\"").append(turnList.get(i))
                    .append("\",\"username\":\"").append(turnUsername)
                    .append("\",\"credential\":\"").append(turnCredential)
                    .append("\"}");
            }
        }
        
        json.append("]");
        return json.toString();
    }
} 